#include <sstream>

#include "cmd_err_code.hpp"
#include "db_cmd.hpp"

namespace cmd
{
	void DBReq::serialize(Streamer & streamer)
	{
		SSHead::serialize(streamer);
		streamer & _should_resp
			     & _op_type
			     & _op_cmd
				 & _sequence;
	}

	const std::string DBReq::to_string() const
	{
		std::ostringstream oss;
		
		oss << SSHead::to_string()
			<< " body: {should_resp: " << (int) _should_resp
			<< ", op_type: " << (int)_op_type
			<< ", op_cmd: " << _op_cmd
			<< ", sequence: " << _sequence
			<< "}";
		
		return oss.str();
	}

	void DBRes::serialize(Streamer & streamer)
	{
		SSHead::serialize(streamer);
		streamer & _err_code;
		if (0 != _err_code)
		{
			return;
		}

		streamer & _affected_rows
				 & _sequence
				 & _result_list;
	}

	const std::string DBRes::to_string() const
	{
		std::ostringstream oss;
		
		oss << SSHead::to_string()
			<< " body: {err_code: " << (int) _err_code << " (" << game_err_to_str(_err_code) << ")";

		if (0 != _err_code)
		{
			oss << "}";
			return oss.str();
		}
		oss << ", affected_rows: " << (int) _affected_rows
			<< ", sequence: " << _sequence
			<< ", vec.size: " << (int) _result_list.size()
			<< "}";

		return oss.str();
	}
	
	void DBGetRoleDataReq::serialize(Streamer & streamer)
	{
		SSHead::serialize(streamer);
		streamer & _rid
			     & _sequence;
	}

	const std::string DBGetRoleDataReq::to_string() const
	{
		std::ostringstream oss;
		oss << SSHead::to_string()
			<< " body: {rid: " << _rid
			<< ", sequence: " << _sequence
			<< "}";
		return oss.str();
	}

	void DBGetRoleDataRes::serialize(Streamer & streamer)
	{
		SSHead::serialize(streamer);
		streamer & _err_code
			     & _sequence
				 & _rid;
		
		if (0 != _err_code)
		{
			return;
		}
		
		streamer & _role_data;
	}

	const std::string DBGetRoleDataRes::to_string() const
	{
		std::ostringstream oss;

		oss << SSHead::to_string()
			<< " body: {err_code: " << (int)_err_code
			<< "(" << game_err_to_str(_err_code) << ")"
			<< ", sequence: " << _sequence
			<< ", rid: " << _rid;

		if (0 == _err_code)
		{
			oss << ", role_data: " << _role_data.to_string();
		}

		oss << "}";

		return oss.str();
	}
};

